<?php
class Order_model extends MY_Model {
    
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    public function list_order()
    {
        $this->db->select();
        $this->db->from('tbl_order');
        $this->db->order_by('order_date', 'DESC');
        $query = $this->db->get();
        return $query->result_array();
    }
    public function order_info($order_id = '')
    {
        $this->db->select('*');
        $this->db->from('tbl_order_detail');
        $this->db->join('tbl_product', "tbl_product.pro_id = tbl_order_detail.pro_id");
        $this->db->where('order_id', $order_id);
        $query = $this->db->get();
        $data = array();
        if($query->num_rows() > 0)
        {
            $data =  $query->result_array();
        }
        return $data;
    }
    public function order_filter($start = '', $end = '')
    {
        $this->db->select();
        $this->db->from('tbl_order');
//        $this->db->where("order_date >= {$start}");
//        $this->db->where("order_date <= {$end}");
        $this->db->where("order_date BETWEEN '{$start}' AND '{$end}'");
        $query = $this->db->get();
        $result = array();
        if($query->num_rows() > 0)
        {
            $result = $query->result_array();
        }
        return $result;
    }
    public function repost()
    {
        $sql = "SELECT tbl_product.pro_id, pro_name, sum(quatity) as qty FROM `tbl_order_detail`
                INNER JOIN tbl_product
                    ON  tbl_product.pro_id = tbl_order_detail.pro_id
                GROUP BY tbl_order_detail.pro_id 
                ORDER BY qty DESC
                LIMIT 0,3";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    
     
}